package com.fit.americanChoppers.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.americanChoppers.interfaces.User;
import com.fit.americanChoppers.service.Users;

public class Login {
	private String userName;
	private String password;

	@Component
	private Form loginForm;

	@Inject
	private Messages messages;

	@Inject
	@Property
	private Users users;

	@ApplicationState
	private User user;

	Object onSuccess() {
		
		return Index.class;
	}

	void onValidateForm() {
		System.out.println("In onValidate");
		User authenticatedUser = Security.authenticate(userName, password,
				users);
		if (authenticatedUser != null) {
			user = authenticatedUser;
		} else {
			System.out.println("authentication-failed");
			loginForm.recordError(messages.get("authentication-failed"));
		}
	}

	public String getOrRegisterLabel() {
		return messages.get("or-register");
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
